package com.elvin.platform.socketIO;


import io.socket.client.IO;
import io.socket.client.Socket;

import java.util.Arrays;

/**
 * Description <P> TODO : socket.io client端 <P>
 *
 * @author DASNNJ <a href="mailto:dasnnj@outlook.com"> dasnnj@outlook.com </a>
 * @date 2019-04-27 18:32
 */
public class Client {

  public static void main(String[] args) {
   //String url = "http://81.68.121.222:7001";
    String url = "http://localhost:7001";
    try {
      IO.Options options = new IO.Options();
      options.transports = new String[]{"websocket"};
      //失败重试次数
      options.reconnectionAttempts = 10;
      //失败重连的时间间隔
      options.reconnectionDelay = 1000;
      //连接超时时间(ms)
      options.timeout = 500;
      final Socket socket = IO.socket(url, options);
      //监听自定义msg事件
      socket.on("res", objects -> System.out.println("client: 服务端发送的消息->" + Arrays.asList(objects)));
      //监听自定义订阅事件
      socket.on("msg", objects -> System.out.println("client: " + "订阅成功，收到反馈->" + Arrays.asList(objects)));

      socket.on(Socket.EVENT_CONNECT, objects -> {
        socket.emit("msg","你好，我是SocketIo客户端！");
        System.out.println("client: " + "连接成功");
      });
      socket.on(Socket.EVENT_CONNECTING, objects -> System.out.println("client: " + "连接中"));
      socket.on(Socket.EVENT_CONNECT_TIMEOUT, objects -> System.out.println("client: " + "连接超时"));
      socket.on(Socket.EVENT_CONNECT_ERROR, objects -> System.out.println("client: " + "连接失败"));
      socket.connect();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
